package day09;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {

    public static void main(String[] args) {

        readAndWriter();
    }

    public static void read() {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile("src/test11/hello.txt", "r");
            int read;
            while ((read = raf.read()) != -1) {
                System.out.print((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readAndWriter() {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile("src/test11/hello.txt", "rw");
            raf.seek(5);
            raf.write("xyz".getBytes());
            long length = raf.length();
            System.out.println(length);
            raf.getChannel();
//            raf.write(2222);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
